package com.example.finance_oa.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.comment_oa.pojos.Additional;
import com.example.comment_oa.pojos.Budget;
import com.example.comment_oa.pojos_vo.AdditionalVo;
import com.example.comment_oa.pojos_vo.AdjustVo;
import com.example.finance_oa.mapper.ProcessMapper;
import com.example.finance_oa.service.BudgetService;
import com.example.finance_oa.mapper.BudgetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Jin
 * @description 针对表【budget】的数据库操作Service实现
 * @createDate 2022-10-25 10:26:37
 */
@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget>
        implements BudgetService{
    @Autowired
    BudgetMapper budgetMapper;
    @Autowired
    ProcessMapper processMapper;

    @Override
    public List<Budget> allBudget(Budget budget) {
        return budgetMapper.allBudget(budget);
    }

    @Override
    public List<Budget> allBudgetKm() {
        return budgetMapper.findAllByBudgetKm();
    }

    @Override
    public List<Budget> allXx(Budget budget) {
        return budgetMapper.allXx(budget);
    }



    @Override
    public int updateZjys(AdditionalVo additionalVo) {
        if(additionalVo.getProcessState()==1) {
            Budget budget = new Budget();
            Date date = new Date(String.valueOf(additionalVo.getAdditionalPeriod()));
            String year = DateUtil.format(date, "yyyy");
            String month = DateUtil.format(date, "MM");
            budget.setBudgetKm(additionalVo.getAdditionalSubjects());
            budget.setBudgetCsmoney(additionalVo.getTzje());
            budget.setBudgetSymoney(additionalVo.getTzje());
            budget.setDeptName(additionalVo.getDeptName());
            budget.setBudgetYear(year);
            budget.setBudgetMonth(month);
            budgetMapper.updateBudget(budget);
            processMapper.updateProcessState(additionalVo.getProcessBh());
            System.err.println(additionalVo.getAdditionalPeriod());
        }else if(additionalVo.getProcessState()==2){
            processMapper.updateProcessState1(additionalVo.getProcessBh());
        }

        return 0;
    }

    @Override
    public int updateBgys(AdjustVo adjustVo) {
        if(adjustVo.getProcessState()==1) {
            Date date = new Date(String.valueOf(adjustVo.getChangePeriod()));
            String year = DateUtil.format(date, "yyyy");
            String month = DateUtil.format(date, "MM");
            Date date1 = new Date(String.valueOf(adjustVo.getOutPeriod()));
            String year1 = DateUtil.format(date1, "yyyy");
            String month1 = DateUtil.format(date1, "MM");
            Budget budget = new Budget();
            Budget budget1 = new Budget();
            budget.setBudgetCsmoney(adjustVo.getTzje());
            budget.setBudgetSymoney(adjustVo.getTzje());
            budget.setBudgetKm(adjustVo.getChangeSubjects());
            budget.setDeptName(adjustVo.getChangeDept());
            budget.setBudgetYear(year);
            budget.setBudgetMonth(month);
            budget1.setBudgetKm(adjustVo.getOutSubjects());
            budget1.setDeptName(adjustVo.getOutDept());
            budget1.setBudgetYear(year1);
            budget1.setBudgetMonth(month1);
            budget1.setBudgetCsmoney(adjustVo.getTzje());
            budget1.setBudgetSymoney(adjustVo.getTzje());
            budgetMapper.updateBudget(budget);
            budgetMapper.updateBudget1(budget1);
            processMapper.updateProcessState(adjustVo.getProcessBh());
        }else if(adjustVo.getProcessState()==2){
            processMapper.updateProcessState1(adjustVo.getProcessBh());
        }


        return 0;
    }

    @Override
    public List<Budget> selectYear(String year, String month) {
        return budgetMapper.selectByyear(year,month);
    }
}




